package com.hgy.carsb.service;

import com.hgy.carsb.dao.DataMapper;
import com.hgy.carsb.entity.EveryMonthData;
import com.hgy.carsb.entity.Total;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.BigInteger;

@Service
public class OverlayInfoService {

    @Autowired
    private InfoService infoService;

    public Total getTotal(int year, int month, String zbCode, String prefix){
        Total data = new Total();
        String lastYearSum = infoService.getYearSum(year-1, month, zbCode, prefix);
        String lastMonth = "0";
        String thisMonth = infoService.getThisMonth(year, month, zbCode, prefix);
        if (month==1){
            lastMonth = infoService.getThisMonth(year-1, 12, zbCode, prefix);
        }else {
            lastMonth = infoService.getThisMonth(year, month-1, zbCode, prefix);
        }
        String thisYearSum = infoService.getYearSum(year, month, zbCode, prefix);
        data.setSum(thisYearSum);
        BigDecimal lastYearNum = new BigDecimal(lastYearSum);
        BigDecimal lastMonthNum = new BigDecimal(lastMonth);
        BigDecimal thisMonthNum = new BigDecimal(thisMonth);
        BigDecimal thisYearNum = new BigDecimal(thisYearSum);
        BigDecimal subMonth = thisMonthNum.subtract(lastMonthNum);
        BigDecimal subYear = thisYearNum.subtract(lastYearNum);
        if (lastMonthNum.compareTo(new BigDecimal(0))==0){
            subMonth = new BigDecimal(0);
        }else {
            subMonth = subMonth.multiply(new BigDecimal(100)).divide(lastMonthNum, 2,BigDecimal.ROUND_HALF_UP);
        }
        if (lastYearNum.compareTo(new BigDecimal(0))==0){
            subYear = new BigDecimal(0);
        }else {
            subYear = subYear.multiply(new BigDecimal(100)).divide(lastYearNum, 2,BigDecimal.ROUND_HALF_UP);
        }
        if (subMonth.compareTo(new BigDecimal(0))<0){
            data.setMonthFlag(false);
        }else {
            data.setMonthFlag(true);
        }
        if (subYear.compareTo(new BigDecimal(0))<0){
            data.setYearFlag(false);
        }else {
            data.setYearFlag(true);
        }
        data.setMonthRate(subMonth.abs().toString());
        data.setYearRate(subYear.abs().toString());
        return data;
    }
}
